const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
const {
  mock_flag,
  login_flag
} = require("../../../data/index.js")
Page({
  data: {
    //用户id
    userId: 0,
    //维修报备id
    repairReportId: '',
    // 评分范围
    scoreRange: ['1分', '2分', '3分', '4分', '5分'],
    // 选中的评分
    selectedScore: '请选择评分',
    // 评价内容
    evaluationContent: ''
  },
  onLoad(options) {
    //登录拦截
    const isLogin = wx.getStorageSync('isLogin');
    if (!isLogin) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      if (login_flag) {
        wx.redirectTo({
          url: '/pages/user/wxlogin/wxlogin'
        });
      }
      if (!login_flag) {
        wx.redirectTo({
          url: '/pages/user/login/login'
        });
      }
    }
    //初始化数据
    this.setData({
      userId: wx.getStorageSync('userId'), //获取用户id
      repairReportId: options.id,
    })
    //mock数据
    if (mock_flag) {
      this.setData({

      })
    }
    //网络请求
    if (!mock_flag) {

    }
  },
  /**
   * 评价表单
   */
  // 评分选择改变事件
  onScoreChange: function (e) {
    const index = e.detail.value;
    const selectedScore = this.data.scoreRange[index];
    this.setData({
      selectedScore: selectedScore
    });
  },
  // 评价内容输入事件
  onContentInput: function (e) {
    this.setData({
      evaluationContent: e.detail.value
    });
  },
  // 提交评价按钮点击事件
  onSubmit: function () {
    if (this.data.selectedScore === '请选择评分') {
      wx.showToast({
        title: '请选择评分',
        icon: 'none'
      });
      return;
    }
    if (this.data.evaluationContent === '') {
      wx.showToast({
        title: '请输入评价内容',
        icon: 'none'
      });
      return;
    }
    // 提交评价
    if (mock_flag) {
      wx.showToast({
        title: '评价提交成功',
        icon: 'success'
      });
      //跳转首页
      wx.switchTab({url: '/pages/work/index/index',})
    }
    if (!mock_flag) {
      this.http_repairEvaluation_add()
    }
  },
  /**
   * 网络请求
   */
  //查询用户详情
  http_repairEvaluation_add() {
    postRequest(baseUrl + "/front/repairEvaluation/add", {
      repairEvaluationScore: this.data.selectedScore,
      repairEvaluationContent: this.data.evaluationContent,
      repairReportId: this.data.repairReportId,
      studentId: this.data.userId
    }).then(res => {
      //console.log(res.data)
      if (res.code == 200) {
        wx.showToast({
          title: '评价提交成功',
          icon: 'success'
        });
        //跳转首页
        wx.switchTab({url: '/pages/work/index/index',})
      }
    })
  },
});